#!/bin/bash

# 物业保洁系统 - 龙蜥7.9一键部署脚本
# 适用于：Anolis OS 7.9 (兼容CentOS 7)

set -e

echo "========================================"
echo "物业保洁系统 - 龙蜥7.9部署脚本"
echo "服务器：10.158.94.94"
echo "========================================"
echo

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查是否为root用户
if [[ $EUID -ne 0 ]]; then
   echo -e "${RED}错误: 请使用root用户运行此脚本${NC}"
   exit 1
fi

echo -e "${GREEN}[1/6] 检查系统环境...${NC}"

# 检查系统版本
cat /etc/os-release | grep -i anolis
if [ $? -ne 0 ]; then
    echo -e "${YELLOW}警告: 非龙蜥系统，但CentOS 7兼容${NC}"
fi

# 检查系统架构
arch=$(uname -m)
echo "系统架构: $arch"

# 更新系统
echo -e "${GREEN}[2/6] 更新系统软件包...${NC}"
yum update -y
yum install -y epel-release
yum install -y wget curl vim net-tools

echo -e "${GREEN}[3/6] 安装Docker...${NC}"

# 安装Docker依赖
yum install -y yum-utils device-mapper-persistent-data lvm2

# 添加Docker仓库
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

# 安装Docker
yum install -y docker-ce docker-ce-cli containerd.io

# 启动Docker服务
systemctl start docker
systemctl enable docker

# 检查Docker状态
if systemctl is-active --quiet docker; then
    echo -e "${GREEN}✅ Docker安装成功${NC}"
else
    echo -e "${RED}❌ Docker安装失败${NC}"
    exit 1
fi

echo -e "${GREEN}[4/6] 安装Docker Compose...${NC}"

# 安装Docker Compose
curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

# 创建符号链接
ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose

# 验证安装
docker-compose --version

echo -e "${GREEN}[5/6] 配置防火墙...${NC}"

# 开放必要端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --permanent --add-port=3000/tcp
firewall-cmd --reload

echo -e "${GREEN}[6/6] 部署物业保洁系统...${NC}"

# 创建项目目录
mkdir -p /opt/property-cleaning-system
cd /opt/property-cleaning-system

# 创建docker-compose.yml
cat > docker-compose.yml << 'EOF'
version: '3.8'

services:
  mysql:
    image: mysql:8.0
    container_name: property-mysql
    environment:
      MYSQL_ROOT_PASSWORD: root123
      MYSQL_DATABASE: property_cleaning
      MYSQL_USER: property_user
      MYSQL_PASSWORD: property_pass
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
      - ./database/schema.sql:/docker-entrypoint-initdb.d/schema.sql
    command: --default-authentication-plugin=mysql_native_password

  backend:
    image: openjdk:17-jdk-slim
    container_name: property-backend
    working_dir: /app
    volumes:
      - ./backend:/app
    ports:
      - "8080:8080"
    depends_on:
      - mysql
    environment:
      - SPRING_PROFILES_ACTIVE=prod
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/property_cleaning?useSSL=false&serverTimezone=UTC
      - SPRING_DATASOURCE_USERNAME=property_user
      - SPRING_DATASOURCE_PASSWORD=property_pass
    command: ./mvnw spring-boot:run

  frontend:
    image: node:18-alpine
    container_name: property-frontend
    working_dir: /app
    volumes:
      - ./frontend:/app
    ports:
      - "3000:3000"
    depends_on:
      - backend
    command: sh -c "npm install && npm run dev -- --host 0.0.0.0"

volumes:
  mysql_data:
EOF

# 复制项目文件
echo "正在复制项目文件..."
if [ -d "backend" ]; then
    cp -r backend/* /opt/property-cleaning-system/backend/
fi
if [ -d "frontend" ]; then
    cp -r frontend/* /opt/property-cleaning-system/frontend/
fi
if [ -d "database" ]; then
    cp -r database/* /opt/property-cleaning-system/database/
fi

# 设置权限
chmod -R 755 /opt/property-cleaning-system

# 创建启动脚本
cat > start-system.sh << 'EOF'
#!/bin/bash
cd /opt/property-cleaning-system
echo "正在启动物业保洁系统..."
docker-compose up -d
echo "等待服务启动..."
sleep 30
echo "系统启动完成！"
echo "访问地址："
echo "- 前端：http://10.158.94.94:3000"
echo "- 后端API：http://10.158.94.94:8080"
echo "- API文档：http://10.158.94.94:8080/swagger-ui.html"
EOF

chmod +x start-system.sh

# 创建停止脚本
cat > stop-system.sh << 'EOF'
#!/bin/bash
cd /opt/property-cleaning-system
echo "正在停止系统..."
docker-compose down
echo "系统已停止"
EOF

chmod +x stop-system.sh

# 创建状态检查脚本
cat > status.sh << 'EOF'
#!/bin/bash
cd /opt/property-cleaning-system
echo "系统状态检查："
echo "=================="
docker-compose ps

echo
echo "端口状态："
echo "=================="
netstat -tlnp | grep -E ':(3000|8080|3306)'

echo
echo "服务健康检查："
echo "=================="
curl -s -o /dev/null -w "%{http_code}" http://localhost:3000 | grep -q "200" && echo "✅ 前端服务正常" || echo "❌ 前端服务异常"
curl -s -o /dev/null -w "%{http_code}" http://localhost:8080 | grep -q "200" && echo "✅ 后端服务正常" || echo "❌ 后端服务异常"
EOF

chmod +x status.sh

echo
echo "========================================"
echo "部署完成！"
echo "========================================"
echo
echo "项目目录：/opt/property-cleaning-system"
echo
echo "启动命令："
echo "  ./start-system.sh"
echo
echo "停止命令："
echo "  ./stop-system.sh"
echo
echo "状态检查："
echo "  ./status.sh"
echo
echo "访问地址："
echo "  前端：http://10.158.94.94:3000"
echo "  后端API：http://10.158.94.94:8080"
echo "  API文档：http://10.158.94.94:8080/swagger-ui.html"
echo
echo "数据库连接信息："
echo "  主机：10.158.94.94"
echo "  端口：3306"
echo "  数据库：property_cleaning"
echo "  用户：property_user"
echo "  密码：property_pass"
echo
echo "如需远程访问，请确保防火墙已开放相应端口"